package com.zjzywl.iotSendthings.controller;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zjzywl.iotSendthings.entity.JDWPBGMX;
import com.zjzywl.iotSendthings.entity.JDWPBGXX;
import com.zjzywl.iotSendthings.entity.WPJSZPB;
import com.zjzywl.iotSendthings.entity1.KSSRYJBXXB;
import com.zjzywl.iotSendthings.entity1.RYJBXXB;
import com.zjzywl.iotSendthings.entity1.KSSRYBDXXB;
import com.zjzywl.iotSendthings.repository.InsertWPJSZPBRepository;
import com.zjzywl.iotSendthings.repository.ProvideInformationRepository;
import com.zjzywl.iotSendthings.repository.ProvideJDWPBGMXRepository;
import com.zjzywl.iotSendthings.repository1.KsszpbRepository;
import com.zjzywl.iotSendthings.repository1.PrisonerRepository;
import com.zjzywl.iotSendthings.util.WebResult;

@Controller
@RequestMapping(value = "/provide")
public class ProvideInformationController extends BaseController {
    @Autowired
    private ProvideInformationRepository provideRepository;
    @Autowired
    private ProvideJDWPBGMXRepository JDWPBGMXRepository;
    @Autowired
    private PrisonerRepository prisonerRepository;
    @Autowired
    private KsszpbRepository sszpbRepository;
    @Autowired
    private InsertWPJSZPBRepository WPJSZPBRepository;

    /*
     * 提供当前送物人姓名和身份证字段
     */
    @RequestMapping(value = "/information")
    @ResponseBody
    public WebResult getInformation(HttpServletRequest request) {
        String uuid = request.getParameter("uuid");
        List<String> findByIp = provideRepository.findByIp(uuid);
        return WebResult.OK(findByIp);
    }

    /*
     * 提供后台送物人员信息
     */
    @RequestMapping(value = "/backinfo")
    @ResponseBody
    public WebResult getbackInformation(HttpServletRequest request) {
        String uuid = request.getParameter("uuid");
        JDWPBGXX info = provideRepository.findByUuid(uuid);
        Map<String, String> map = new HashMap<>();
        map.put("sjc", info.getSjc());
        map.put("swrxm", info.getSwrxm());
        map.put("swrzjh", info.getSwrzjh());
        map.put("swrdz", info.getSwrdz());
        map.put("lxdh", info.getLxdh());
        map.put("djbh", info.getDjbh());
        map.put("yzyrygx", info.getYzyrygx());
        String rybh = info.getRybh();
        KSSRYJBXXB jb = prisonerRepository.getByJsh(rybh);
        map.put("jsh", jb.getJsh());
        String jbxxbh = jb.getJbxxbh();
        RYJBXXB zjhm = prisonerRepository.getZjhm(jbxxbh);
        map.put("name", zjhm.getXm());
        map.put("zjhm", zjhm.getZjhm());
        KSSRYBDXXB rssj = prisonerRepository.getRssj(rybh);
        map.put("rssj", rssj.getRsrq());
        String bgr = (String) request.getSession().getAttribute("username");
        map.put("bgr", bgr);
        return WebResult.OK(map);
    }

    /*
     * 提供所有送物人信息
     */
    @RequestMapping(value = "/info")
    @ResponseBody
    public WebResult getInfo() {
        List<JDWPBGXX> findByIp = provideRepository.findlist();
        return WebResult.OK(findByIp);
    }

    /*
     * 提供当前送物信息
     *
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public WebResult getThings(HttpServletRequest request) {
        String uuid = request.getParameter("uuid");
        System.out.println(uuid);
        List<JDWPBGMX> list = JDWPBGMXRepository.findByUuid(uuid);
        return WebResult.OK(list);
    }

    /*
     * 获取犯人图片
     */
    @RequestMapping(value = "/getPrisonerImg")
    public Object doGetBackgroundImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String zjhm = request.getParameter("sfz");
        List<RYJBXXB> prisoners = prisonerRepository.getName(zjhm);
        RYJBXXB prisoner = prisoners.get(0);
        String jbxxbh = prisoner.getJbxxbh();
        KSSRYJBXXB pri = prisonerRepository.getJsh(jbxxbh);
        String rybh = pri.getRybh();
        byte[] device = sszpbRepository.getImg(rybh);
        response.setContentType("image/jpeg ");
        OutputStream output = response.getOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(device);// 获取实体类对应Byte
        int len;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf)) != -1) {
            output.write(buf, 0, len);
        }
        output.flush();
        output.close();
        return null;
    }

    /*
     * 获取历史图片数量
     */
    @RequestMapping(value = "/getImgNum")
    @ResponseBody
    public WebResult getImgNum(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String uuid = request.getParameter("uuid");
        List<WPJSZPB> img = WPJSZPBRepository.getImgNum(uuid);
        if (img == null) {
            return WebResult.FAIL("图片不存在");
        }
        return WebResult.OK(img.size());
    }

    /*
     * 获取历史中展示的图片
     */
    @RequestMapping(value = "/getThingImg")
    public Object doGetThingImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String uuid = request.getParameter("uuid");
        String xh = request.getParameter("xh");
        List<WPJSZPB> list = WPJSZPBRepository.getImg(uuid, xh);
        for (WPJSZPB wpjszpb : list) {
            Blob device = wpjszpb.getZp();
            byte[] ByteArr = blobToBytes(device);
            response.setContentType("image/jpeg ");
            OutputStream output = response.getOutputStream();
            ByteArrayInputStream in = new ByteArrayInputStream(ByteArr);// 获取实体类对应Byte
            int len;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                output.write(buf, 0, len);
            }
            output.flush();
            output.close();
        }
        return null;
    }

    /*
     * 提供囚犯的信息 ：姓名+基本信息编号
     */
    @RequestMapping(value = "/getPrisonerInfo")
    @ResponseBody
    public WebResult getPrisonerInfo(HttpServletRequest request) {
        String zjhm = request.getParameter("zjhm");
        List<RYJBXXB> prisoners = prisonerRepository.getName(zjhm);
        RYJBXXB prisoner = prisoners.get(0);
        KSSRYJBXXB KSSRYJBXXB = prisonerRepository.getByJbxxbh(prisoner.getJbxxbh());
        if (!KSSRYJBXXB.getRybj().equals("0")) {
            return WebResult.FAIL("查无此人");
        }
        String name = prisoner.getXm();
        String jbxxbh = prisoner.getJbxxbh();
        Map<String, String> map = new HashMap<>();
        map.put("name", name);
        KSSRYJBXXB pri = prisonerRepository.getJsh(jbxxbh);
        String jsh = pri.getJsh();
        String rybh = pri.getRybh();
        map.put("jsh", jsh);
        map.put("zjhm", zjhm);
        map.put("rybh", rybh);
        KSSRYBDXXB rssj = prisonerRepository.getRssj(rybh);
        map.put("rssj", rssj.getRsrq());
        return WebResult.OK(map);

    }

    private byte[] blobToBytes(Blob blob) {
        BufferedInputStream is = null;
        try {
            is = new BufferedInputStream(blob.getBinaryStream());
            byte[] bytes = new byte[(int) blob.length()];
            int len = bytes.length;
            int offset = 0;
            int read = 0;
            while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {
                offset += read;
            }
            return bytes;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                is.close();
                is = null;
            } catch (IOException e) {
                return null;
            }
        }
    }
}
